﻿package mortal.game.view.shopMall.data
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import flash.utils.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;

    public class ShopUtil extends Object
    {
        public static const ShopTabHotBuy:String = "m0";
        public static const ShopTabProShop:String = "m1";
        public static const ShopTabGoldShop:String = "m2";
        public static const ShopTabGoldBindShop:String = "m3";
        public static const ShopTabWarHonorShop:String = "m4";
        public static const ShopTabMeritShop:String = "m5";
        public static const ShopTabArena1v1:String = "m6";
        public static const ShopTabChallenge:String = "m7";
        public static const ShopTabArena2v2:String = "m8";
        private static var _shopMallTab:Array;
        private static var _getTabLevel:int;

        public function ShopUtil()
        {
            return;
        }// end function

        private function isHasHighterItemByPackPosType(param1:int, param2:ItemData) : Boolean
        {
            var _loc_3:Boolean = false;
            var _loc_4:ItemData = null;
            var _loc_5:* = Cache.instance.pack.getPackChacheByPosType(param1);
            if (_loc_5)
            {
                _loc_4 = _loc_5.isHasBetterInPack(param2);
            }
            return _loc_3;
        }// end function

        public static function getShopMallTab() : Array
        {
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            if (_getTabLevel == 0 || _getTabLevel != _loc_1)
            {
                _getTabLevel = _loc_1;
                _shopMallTab = [];
                addShop(ShopTabData.typeRebateShop, 0, "ShopMall_1", ShopTabHotBuy, 6);
                addShop(ShopTabData.typeCommonShop, 1003, "ShopMall_3", ShopTabProShop, 12);
                addShop(ShopTabData.typeCommonShop, 1005, "ShopMall_5", ShopTabGoldShop, 12);
                addShop(ShopTabData.typeCommonShop, 1006, "ShopMall_7", ShopTabGoldBindShop, 12);
                addShop(ShopTabData.typeCopyShop, 3001, "ShopMall_6", ShopTabWarHonorShop, 12);
                addShop(ShopTabData.typeCopyShop, 3008, "ShopMall_2", ShopTabMeritShop, 12);
                addShop(ShopTabData.typeGroupShop, 0, "ShopMall_4", ShopTabArena1v1, 9, get1V1ArenaShopConfig);
                if (_loc_1 >= 70)
                {
                    addShop(ShopTabData.typeGroupShop, 0, "ShopMall_9", ShopTabArena2v2, 9, get2V2ArenaShopConfig);
                }
                addShop(ShopTabData.typeGroupShop, 0, "ShopMall_8", ShopTabChallenge, 9, getExchangeShopConfig);
            }
            return _shopMallTab;
        }// end function

        private static function addShop(param1:int, param2:int, param3:String, param4:String, param5:int, param6:Function = null) : ShopTabData
        {
            var _loc_7:* = new ShopTabData();
            _loc_7.type = param1;
            _loc_7.code = param2;
            _loc_7.styleName = param3;
            _loc_7.tabIndexName = param4;
            _loc_7.showItemNum = param5;
            _loc_7.getItemListCallBack = param6;
            _shopMallTab.push(_loc_7);
            return _loc_7;
        }// end function

        public static function getTabIndexByTabName(param1:String) : int
        {
            var _loc_3:int = 0;
            var _loc_4:ShopTabData = null;
            var _loc_5:int = 0;
            var _loc_2:* = getShopMallTab();
            while (_loc_5 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_5];
                if (_loc_4.tabIndexName == param1)
                {
                    _loc_3 = _loc_5;
                    break;
                }
                _loc_5++;
            }
            return _loc_3;
        }// end function

        public static function getTabDataByTabName(param1:String) : ShopTabData
        {
            var _loc_3:int = 0;
            var _loc_4:ShopTabData = null;
            var _loc_5:int = 0;
            var _loc_2:* = getShopMallTab();
            while (_loc_5 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_5];
                if (_loc_4.tabIndexName == param1)
                {
                    break;
                }
                else
                {
                    _loc_4 = null;
                }
                _loc_5++;
            }
            return _loc_4;
        }// end function

        public static function getItemListByTabNameAndSecIndex(param1:String, param2:int = 0) : Array
        {
            var _loc_4:Vector.<TShopSell> = null;
            var _loc_5:int = 0;
            var _loc_8:Array = null;
            var _loc_9:TShop = null;
            var _loc_10:TShopSell = null;
            var _loc_11:TShopSell = null;
            var _loc_3:Array = [];
            var _loc_6:* = getShopMallTab();
            var _loc_7:* = getTabDataByTabName(param1);
            if (_loc_7)
            {
                if (_loc_7.type == ShopTabData.typeRebateShop)
                {
                    _loc_3 = _loc_3.concat(ShopConfig.instance.rebateShopMap[Cache.instance.shop.curShopId]);
                }
                else if (_loc_7.type == ShopTabData.typeGroupShop)
                {
                    _loc_8 = _loc_7.getItemListCallBack.apply() as Array;
                    _loc_9 = _loc_8[param2];
                    _loc_4 = ShopConfig.instance.getShopSellItemByShopCode(_loc_9.code);
                    for each (_loc_10 in _loc_4)
                    {
                        
                        _loc_3.push(_loc_10);
                    }
                }
                else
                {
                    _loc_5 = _loc_7.code;
                    _loc_4 = ShopConfig.instance.getShopSellItemByShopCode(_loc_5);
                    for each (_loc_11 in _loc_4)
                    {
                        
                        _loc_3.push(_loc_11);
                    }
                }
            }
            return _loc_3;
        }// end function

        public static function getShopMallTabObjByShopCode(param1:int) : ShopTabData
        {
            var _loc_2:ShopTabData = null;
            var _loc_4:ShopTabData = null;
            var _loc_5:Array = null;
            var _loc_6:TShop = null;
            var _loc_3:* = getShopMallTab();
            if (param1 == 0)
            {
                _loc_2 = _loc_3[0];
            }
            else
            {
                for each (_loc_4 in _loc_3)
                {
                    
                    if (_loc_4.code == param1)
                    {
                        _loc_2 = _loc_4;
                        break;
                        continue;
                    }
                    if (_loc_4.type == ShopTabData.typeGroupShop)
                    {
                        _loc_5 = _loc_4.getItemListCallBack.apply() as Array;
                        for each (_loc_6 in _loc_5)
                        {
                            
                            if (param1 == _loc_6.code)
                            {
                                _loc_2 = _loc_4;
                                break;
                            }
                        }
                    }
                }
            }
            return _loc_2;
        }// end function

        public static function isShopMallShopCode(param1:int) : Boolean
        {
            var _loc_2:* = ShopConfig.instance.getTShopByCode(param1);
            if (_loc_2 && _loc_2.isMall == 1000)
            {
                return true;
            }
            return false;
        }// end function

        public static function getShopMallItems() : Array
        {
            var _loc_2:Vector.<TShopSell> = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Array = null;
            var _loc_7:int = 0;
            var _loc_8:TShop = null;
            var _loc_9:TShopSell = null;
            var _loc_10:TShopSell = null;
            var _loc_1:Array = [];
            var _loc_6:* = getShopMallTab();
            while (_loc_7 < _loc_6.length)
            {
                
                _loc_4 = (_loc_6[_loc_7] as ShopTabData).type;
                if (_loc_4 == ShopTabData.typeRebateShop)
                {
                    _loc_1 = _loc_1.concat(ShopConfig.instance.rebateShopMap[Cache.instance.shop.curShopId]);
                }
                else if (_loc_4 == ShopTabData.typeGroupShop)
                {
                    _loc_5 = (_loc_6[_loc_7] as ShopTabData).getItemListCallBack.apply() as Array;
                    for each (_loc_8 in _loc_5)
                    {
                        
                        _loc_2 = ShopConfig.instance.getShopSellItemByShopCode(_loc_8.code);
                        for each (_loc_9 in _loc_2)
                        {
                            
                            _loc_1.push(_loc_9);
                        }
                    }
                }
                else
                {
                    _loc_3 = (_loc_6[_loc_7] as ShopTabData).code;
                    _loc_2 = ShopConfig.instance.getShopSellItemByShopCode(_loc_3);
                    for each (_loc_10 in _loc_2)
                    {
                        
                        _loc_1.push(_loc_10);
                    }
                }
                _loc_7++;
            }
            return _loc_1;
        }// end function

        private static function get2V2ArenaShopConfig() : Array
        {
            var _loc_4:TArenaConfig = null;
            var _loc_5:Dictionary = null;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            var _loc_8:TShop = null;
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            var _loc_2:Array = [];
            var _loc_3:* = ArenaConfig.instance.getConfig;
            for each (_loc_4 in _loc_3)
            {
                
                if (_loc_4.copyType == ECopyType._ECopyTypeArena2v2 && _loc_4.type == 2 && _loc_4.cond <= _loc_1)
                {
                    _loc_2.push(_loc_4);
                }
            }
            _loc_5 = ShopConfig.instance.shopMap;
            _loc_6 = [];
            while (_loc_7 < _loc_2.length)
            {
                
                for each (_loc_8 in _loc_5)
                {
                    
                    if ((_loc_2[_loc_7] as TArenaConfig).valExtend == _loc_8.unit)
                    {
                        _loc_6.push(_loc_8);
                    }
                }
                _loc_7++;
            }
            return _loc_6;
        }// end function

        private static function get1V1ArenaShopConfig() : Array
        {
            var _loc_4:TArenaConfig = null;
            var _loc_5:Dictionary = null;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            var _loc_8:TShop = null;
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            var _loc_2:Array = [];
            var _loc_3:* = ArenaConfig.instance.getConfig;
            for each (_loc_4 in _loc_3)
            {
                
                if (_loc_4.copyType == ECopyType._ECopyTypeArena && _loc_4.type == 2 && _loc_4.cond <= _loc_1)
                {
                    _loc_2.push(_loc_4);
                }
            }
            _loc_5 = ShopConfig.instance.shopMap;
            _loc_6 = [];
            while (_loc_7 < _loc_2.length)
            {
                
                for each (_loc_8 in _loc_5)
                {
                    
                    if ((_loc_2[_loc_7] as TArenaConfig).valExtend == _loc_8.unit)
                    {
                        _loc_6.push(_loc_8);
                    }
                }
                _loc_7++;
            }
            return _loc_6;
        }// end function

        private static function getExchangeShopConfig() : Array
        {
            var _loc_4:ItemData = null;
            var _loc_5:TShop = null;
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            var _loc_2:* = ShopConfig.instance.shopMap;
            var _loc_3:Array = [];
            for each (_loc_5 in _loc_2)
            {
                
                if (_loc_5.type == 2)
                {
                    _loc_4 = new ItemData(_loc_5.unit);
                    if (ItemsUtil.isEquipChip(_loc_4))
                    {
                        if (_loc_1 >= GameDefConfig.instance.getExchangeShopEnterLevel(_loc_5.code))
                        {
                            _loc_3.push(_loc_5);
                        }
                    }
                }
            }
            return _loc_3;
        }// end function

        public static function isEnougthMoney(param1:ShopItemData, param2:Boolean = false) : Boolean
        {
            return Cache.instance.role.enoughMoney(param1.tShop.unit, param1.totalPrize, null, param2);
        }// end function

        public static function get curReBateItem() : RebateItemData
        {
            var _loc_1:* = ShopConfig.instance.rebateShopMap[Cache.instance.shop.curShopId];
            if (_loc_1 == null)
            {
                return null;
            }
            var _loc_2:* = _loc_1[0];
            var _loc_3:* = Cache.instance.shop.getRebateInfoDicByItemCode(_loc_2.itemCode);
            return _loc_3;
        }// end function

        public static function get curRebatItemCanBuyNum() : int
        {
            var _loc_2:int = 0;
            var _loc_1:* = curReBateItem;
            if (_loc_1 && _loc_1.playerInfo)
            {
                _loc_2 = _loc_1.playerInfo.amount;
            }
            else
            {
                _loc_2 = 0;
            }
            return _loc_2;
        }// end function

        public static function getEquipChipSecTabIndexByItem(param1:ItemData) : int
        {
            var _loc_3:TShop = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_2:* = getExchangeShopConfig();
            while (_loc_5 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_5];
                if (_loc_3.unit == param1.itemCode || _loc_3.unit == param1.itemInfo.codeUnbind)
                {
                    _loc_4 = _loc_5;
                    break;
                }
                _loc_5++;
            }
            return _loc_4;
        }// end function

        public static function isHasBuyItem(param1:ItemData) : Boolean
        {
            var _loc_2:Boolean = false;
            var _loc_3:ItemData = null;
            var _loc_4:TSkill = null;
            if (ItemsUtil.isPlayerSkillBook(param1))
            {
                _loc_4 = SkillConfig.instance.getInfoById(param1.itemInfo.effect);
                _loc_2 = Cache.instance.skill.getSkillBySerialId(_loc_4.series) != null;
            }
            else if (ItemsUtil.isEquip(param1))
            {
                _loc_2 = Cache.instance.pack.packRolePackCache.isHasBetterInPack(param1) != null;
            }
            else if (ItemsUtil.isHalidomEquip(param1))
            {
                _loc_2 = Cache.instance.pack.halidomBag.isHasBetterInPack(param1) != null;
            }
            else if (ItemsUtil.isRune(param1.itemInfo))
            {
                _loc_2 = Cache.instance.pack.runeBag.isHasBetterInPack(param1) != null;
            }
            if (!_loc_2)
            {
                _loc_2 = Cache.instance.pack.backPackCache.isHasBetterInPack(param1) != null;
            }
            if (!_loc_2)
            {
                _loc_2 = Cache.instance.pack.warehouseCache.isHasBetterInPack(param1) != null;
            }
            return _loc_2;
        }// end function

    }
}
